package com.asura.app.wechat.ui.moment;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asura.app.library.utils.CollectionUtils;
import com.asura.app.library.utils.StringUtils;
import com.asura.app.wechat.R;
import com.asura.app.wechat.base.BaseRecyclerAdapter;
import com.asura.app.wechat.base.RecyclerHolder;
import com.asura.app.wechat.repository.model.Comment;
import com.asura.app.wechat.repository.model.Image;
import com.asura.app.wechat.repository.model.Tweet;
import com.asura.app.wechat.repository.model.UserInfo;
import com.asura.app.wechat.widget.NetworkImageView;
import com.asura.app.wechat.widget.NineImageGridLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Asura on 2017/12/27.
 */

public class TweetListAdapter extends BaseRecyclerAdapter<Object> {

    private static final int ITEM_TYPE_PROFILE = 1;
    private static final int ITEM_TYPE_LOADING = 2;
    private static final int ITEM_TYPE_TWEET = 3;
    private final int commentNameColor;
    private boolean initLoading;

    public TweetListAdapter(Context context, List<Object> data) {
        super(context, data);
        commentNameColor = ContextCompat.getColor(context, R.color.colorLink);
    }

    @Override
    protected void convert(RecyclerHolder holder, Object o, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case ITEM_TYPE_LOADING:
                convertTweetLoading(holder);
                break;
            case ITEM_TYPE_PROFILE:
                convertProfile(holder, (UserInfo) o, position);
                break;
            case ITEM_TYPE_TWEET:
                convertTweet(holder, (Tweet) o, position);
                break;
        }
    }

    private void convertTweetLoading(RecyclerHolder holder) {
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_loading);
        holder.getView(R.id.iv_loading).startAnimation(animation);
    }

    private void convertProfile(RecyclerHolder holder, UserInfo userInfo, int position) {
        NetworkImageView ivWallpaper = holder.getView(R.id.iv_wallpaper);
        NetworkImageView ivAvatar = holder.getView(R.id.iv_avatar);
        ivWallpaper.load(userInfo.getProfileImage());
        ivAvatar.load(userInfo.getAvatar());
        holder.setText(R.id.tv_name, userInfo.getUserName());
    }

    private void convertTweet(RecyclerHolder holder, Tweet tweet, int position) {
        NetworkImageView ivSenderAvatar = holder.getView(R.id.iv_sender_avatar);
        ivSenderAvatar.load(tweet.getSender().getAvatar());
        holder.setText(R.id.tv_sender_name, tweet.getSender().getUserName());
        holder.setText(R.id.tv_content, tweet.getContent());
        holder.setVisibility(R.id.tv_content, StringUtils.isEmpty(tweet.getContent()) ? View.GONE : View.VISIBLE);
        //图片
        if (CollectionUtils.isEmpty(tweet.getImages())) {
            holder.setVisibility(R.id.iv_auto_size, View.GONE);
            holder.setVisibility(R.id.layout_images, View.GONE);
        } else {
            if (tweet.getImages().size() == 1) {
                holder.setVisibility(R.id.iv_auto_size, View.VISIBLE);
                holder.setVisibility(R.id.layout_images, View.GONE);
                NetworkImageView ivAutoSize = holder.getView(R.id.iv_auto_size);
                ivAutoSize.load(tweet.getImages().get(0).getUrl());
            } else {
                holder.setVisibility(R.id.iv_auto_size, View.GONE);
                holder.setVisibility(R.id.layout_images, View.VISIBLE);
                NineImageGridLayout layoutImages = holder.getView(R.id.layout_images);
                List<String> urls = new ArrayList<>(tweet.getImages().size());
                for (Image image : tweet.getImages()) {
                    urls.add(image.getUrl());
                }
                layoutImages.setImageUrls(urls);
            }
        }
        //评论
        LinearLayout layoutComment = holder.getView(R.id.layout_comment);
        if (CollectionUtils.isEmpty(tweet.getComments())) {
            layoutComment.setVisibility(View.GONE);
        } else {
            layoutComment.setVisibility(View.VISIBLE);
            int childCount = layoutComment.getChildCount();
            int tweetCount = tweet.getComments().size();
            // TODO: 2017/12/29 待优化
            if (childCount < tweetCount) {
                for (int i = childCount; i < tweet.getComments().size(); i++) {
                    View view = LayoutInflater.from(getContext()).inflate(R.layout.item_comment, layoutComment, false);
                    layoutComment.addView(view);
                }
            }
            for (int i = 0; i < tweetCount; i++) {
                Comment comment = tweet.getComments().get(i);
                TextView textView = (TextView) layoutComment.getChildAt(i);
                textView.setVisibility(View.VISIBLE);
                SpannableStringBuilder sb = new SpannableStringBuilder();
                sb.append(comment.getSender().getUserName());
                sb.append(" : ");
                sb.append(comment.getContent());
                sb.setSpan(new ForegroundColorSpan(commentNameColor), 0, comment.getSender().getUserName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                textView.setText(sb);
            }
            for (int i = tweetCount; i < layoutComment.getChildCount(); i++) {
                layoutComment.getChildAt(i).setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected int getLayoutIdFromType(int viewType) {
        switch (viewType) {
            case ITEM_TYPE_PROFILE:
                return R.layout.item_tweet_header_profile;
            case ITEM_TYPE_LOADING:
                return R.layout.item_comment_loading;
            case ITEM_TYPE_TWEET:
                return R.layout.item_tweet;
        }
        return 0;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_PROFILE;
        }
        if (position == 1 && initLoading) {
            return ITEM_TYPE_LOADING;
        }
        return ITEM_TYPE_TWEET;
    }

    public void setInitLoading(boolean firstLoading) {
        this.initLoading = firstLoading;
    }
}
